package com.example.myapplication.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.example.myapplication.common.IAMUserConfig;
import com.example.myapplication.common.http.MyHttp;
import com.example.myapplication.common.http.MyHttpCallback;
import com.example.myapplication.util.LoadingUtil;

public class LoginActivity extends AppCompatActivity implements MyHttpCallback {

    private EditText account;
    private EditText username;
    private EditText password;
    private Button login;
    private MyHttp myHttp;
    private Dialog loadingDialog;

    //用户登录信息
    private String accountstring;
    private String usernamestring;
    private String passwordstring;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        account = (EditText) findViewById(R.id.account);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        loadingDialog = LoadingUtil.createLoadingDialog(this);
        login = (Button) findViewById(R.id.login);
        myHttp = new MyHttp(this, this);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断有效性
                accountstring = account.getText().toString().trim();
                usernamestring = username.getText().toString().trim();
                passwordstring = password.getText().toString().trim();
                if (TextUtils.isEmpty(accountstring) || TextUtils.isEmpty(usernamestring) || TextUtils.isEmpty(passwordstring)) {
                    Toast.makeText(getApplicationContext(), "用户账号信息不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                loadingDialog.show();
                myHttp.queryHttpByLogin("https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens", accountstring,
                        usernamestring, passwordstring, IAMUserConfig.project_id);
            }
        });
    }

    @Override
    public void doMessage(Message message) {
        loadingDialog.dismiss();
        if (message.what == myHttp.LOGIN_RESPONSE) {
            //赋值全局登录参数
            IAMUserConfig.domain_name = accountstring;
            IAMUserConfig.name = usernamestring;
            IAMUserConfig.password = passwordstring;
            //用户登录响应
            String token = (String) message.obj;
            SharedPreferences sp = getSharedPreferences("userconfig", MODE_PRIVATE);
            sp.edit().putString("token", token).commit();
            //进入主界面
            startActivity(new Intent(this, MainActivity.class));
            //从栈中清空
            finish();
        }
    }
}